var _currentIndex = 0;
//页面初始加载
efform_onload = function() {
	var fdPlanStatus = decodeURI($("#fdPlanStatus").val());
	//隐藏/显示从表按钮
	if(fdPlanStatus != '草拟')
		hideAndShowSubButtons(false);
	else
		hideAndShowSubButtons(true);
}; 
//TAB切换
function changeTab(indexclicked)
{
	var info = new EiInfo();
	info.set("fdProduceplanId",$('#fdProduceplanId').val()); 
	info.set("fdProfessionalplanId",$('#fdProfessionalplanId').val()); 
	info.set("fdPlanYear",$('#_planYear').val()); 
	info.set("fdPlanMonth",$('#_planMonth').val());
	if(indexclicked ==0)
	{  
		EiCommunicator.send("ZMCP15", "initLoad", info, ajax_zc_callback);
	}else if(indexclicked ==1)
	{ 
		EiCommunicator.send("ZMCP16", "initLoad", info, ajax_zk_callback);
	}else if(indexclicked ==2)
	{ 
		EiCommunicator.send("ZMCP17", "initLoad", info, ajax_glaz_callback);
	}  
}

function fundiv(currentIndex,index){  
	_currentIndex=index;
	changeTab(index);
}

/**回调*/
//查询钻尺回调
var ajax_zc_callback = {
	onSuccess : function(eiInfo) {
		efwindow.hide();
		var grid = efgrid.getGridObject("ef_grid_zc");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {
		EFAlert("新增失败，原因：" + eMsg);
	}
}
//查询钻孔回调
var ajax_zk_callback = {
	onSuccess : function(eiInfo) {
		efwindow.hide();
		var grid = efgrid.getGridObject("ef_grid_zk");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {
		EFAlert("新增失败，原因：" + eMsg);
	}
}
//查询管路安装回调
var ajax_glaz_callback = {
	onSuccess : function(eiInfo) {
		efwindow.hide();
		var grid = efgrid.getGridObject("ef_grid_glaz");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {
		EFAlert("新增失败，原因：" + eMsg);
	}
}

/************************************************************************************
 * 钻尺从表操作
 *************************************************************************************/
button_insertzc_onclick = function() {
	if (isSelected("ef_grid_zc") == 0) return;
	efgrid.submitForm("ef_grid_zc", "ZM", "ZMCP15", "insert", true);
} 
button_updatezc_onclick = function() {
	if (isSelected("ef_grid_zc") == 0) return; 
	efgrid.submitForm("ef_grid_zc", "ZM", "ZMCP15", "update", true);
} 
button_deletezc_onclick = function() {
	var grid = isSelected("ef_grid_zc");
	if (grid == 0) return;
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if (r) {
			efgrid.submitForm("ef_grid_zc", "ZM", "ZMCP15", "delete", true);
		}
	});
}
button_revertzc_onclick = function (){
	if (isSelected("ef_grid_zc") == 0)return;
	EFConfirm('与所选记录统一批次的数据都会被撤回！', '确认框', function(r) {
		if(r){
			efgrid.submitForm("ef_grid_zc", "ZM", "ZMCP15", "revert", true); 
		}
	});
}
/********************************************************************************
 * 钻孔从表操作
 *********************************************************************************/
button_insertzk_onclick = function() {
	if (isSelected("ef_grid_zk") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_zk", "ZM", "ZMCP16", "insert", true);
} 
button_updatezk_onclick = function() {
	if (isSelected("ef_grid_zk") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_zk", "ZM", "ZMCP16", "update", true);
}
button_deletezk_onclick = function() {
	var grid = isSelected("ef_grid_zk");
	if (grid == 0) {
		return;
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if (r) {
			efgrid.submitForm("ef_grid_zk", "ZM", "ZMCP16", "delete", true);
		}
	});
}
button_revertzk_onclick = function (){
	if (isSelected("ef_grid_zk") == 0)return;
	EFConfirm('与所选记录统一批次的数据都会被撤回！', '确认框', function(r) {
		if(r){
			efgrid.submitForm("ef_grid_zk", "ZM", "ZMCP16", "revert", true); 
		}
	});
}
/********************************************************************************
 * 管路安装从表操作 
 *********************************************************************************/
button_insertglaz_onclick = function() {
	if (isSelected("ef_grid_glaz") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_glaz", "ZM", "ZMCP17", "insert", true);
} 
button_updateglaz_onclick = function() {
	if (isSelected("ef_grid_glaz") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_glaz", "ZM", "ZMCP17", "update", true);
} 
button_deleteglaz_onclick = function() {
	var grid = isSelected("ef_grid_glaz");
	if (grid == 0) {
		return;
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if (r) {
			efgrid.submitForm("ef_grid_glaz", "ZM", "ZMCP17", "delete", true);
		}
	});
}
button_revertglaz_onclick = function (){
	if (isSelected("ef_grid_glaz") == 0)return;
	EFConfirm('与所选记录统一批次的数据都会被撤回！', '确认框', function(r) {
		if(r){
			efgrid.submitForm("ef_grid_glaz", "ZM", "ZMCP17", "revert", true); 
		}
	});
}

/**
 * 从表按钮显示
 */
var hideAndShowSubButtons = function(status) {

	if (!status) {
		efbutton.setButtonStatus("insertzc", false);
		efbutton.setButtonStatus("updatezc", false);
		efbutton.setButtonStatus("deletezc", false);
		efbutton.setButtonStatus("revertzc", false);
		efbutton.setButtonStatus("insertzk", false);
		efbutton.setButtonStatus("updatezk", false);
		efbutton.setButtonStatus("deletezk", false);
		efbutton.setButtonStatus("revertzk", false);
		efbutton.setButtonStatus("insertglaz", false);
		efbutton.setButtonStatus("updateglaz", false);
		efbutton.setButtonStatus("deleteglaz", false);
		efbutton.setButtonStatus("revertglaz", false);
	} else {
		efbutton.setButtonStatus("insertzc", true);
		efbutton.setButtonStatus("updatezc", true);
		efbutton.setButtonStatus("deletezc", true);
		efbutton.setButtonStatus("revertzc", true);
		efbutton.setButtonStatus("insertzk", true);
		efbutton.setButtonStatus("updatezk", true);
		efbutton.setButtonStatus("deletezk", true);
		efbutton.setButtonStatus("revertzk", true);
		efbutton.setButtonStatus("insertglaz", true);
		efbutton.setButtonStatus("updateglaz", true);
		efbutton.setButtonStatus("deleteglaz", true);
		efbutton.setButtonStatus("revertglaz", true);
	}
}

//新增记录行回调函数
efgrid_afterAddNewRow = function(grid_id){ 

	var grid = efgrid.getGridObject(grid_id);
	var rows = grid.getCheckedRows();	
	if(grid_id=="ef_grid_zc")
	{
		for(var i = 0 ; i < rows.length;i++){
			grid.getBlockData().setCell(rows[i], "fdPlanValue", 0);
			grid.getBlockData().setCell(rows[i], "fdZjs", 0);
			grid.getBlockData().setCell(rows[i], "fdRs", 0);
			grid.getBlockData().setCell(rows[i], "fdPlanSx", 0);
			grid.getBlockData().setCell(rows[i], "fdPlanZx", 0);
			grid.getBlockData().setCell(rows[i], "fdPlanXx", 0);
		}
	}
	for(var i = 0 ; i < rows.length;i++){
		grid.getBlockData().setCell(rows[i], "fdKeyId", "");
	}
} 
//提交成功回调函数
efgrid_onAjaxSubmitSuccess = function(gridId, service_name, method_name, eiInfo) {
	if (eiInfo.status != -1) {
		var grid = efgrid.getGridObject(gridId);
		grid.refresh( eiInfo );
	}
	else
	{
		submitCallBackEiMsg = eiInfo.msg;
	}
};

//单元格编辑后回调的方法
efgrid_onDataCellSaved = function( grid_id, row_index, col_index, cell_value ) 
{
	if(grid_id == "ef_grid_zc"){
	  var grid = efgrid.getGridObject( grid_id ); 
	  if( col_index == 7 || col_index == 8 || col_index == 9) 
	  { 	
		  var sx = grid.getCellValueByColumnName(row_index,"fdPlanSx");
		  var zx = grid.getCellValueByColumnName(row_index,"fdPlanZx");
		  var xx = grid.getCellValueByColumnName(row_index,"fdPlanXx");
		  var planValue = Number(sx)+Number(zx)+Number(xx)
		  grid.setCellValue(row_index,2,Number(planValue),TYPE_DATA);
		  grid.refreshCell( row_index, 2 , TYPE_DATA ); 
	  }
	}
	if(grid_id == "ef_grid_zk"){
		var grid = efgrid.getGridObject( grid_id ); 
		if( col_index == 3 || col_index == 4) 
		{ 
			var sl = grid.getCellValueByColumnName(row_index, "fdHoleLength");//数量
			var ks = grid.getCellValueByColumnName(row_index, "fdKs");//孔深(米)
			grid.setCellValue(row_index,5,Number(sl)*Number(ks),TYPE_DATA);
			grid.refreshCell( row_index, 5 , TYPE_DATA ); 
		}
	}
}
